import {
	object,
	array
} from '@/lib/utils/validate';
import { Token } from '../utils/storage';
export default {
	data() {
		return {
			pageDatas: [],
			list: [],
			showDialog: false,
			type: '',
			row: {},
			rows: [],
			formData: {
				value: "",
				page: 1,
				size: 20,
				total: 0
			},
			searchUrl: "",
			deleteUrl: "",
			key: 'id',
			user: {},
		}
	},
	created() {
		try {
			const datas = this.$root._route.meta.datas;
			this.pageDatas = datas;
		} catch (e) {
			this.openMessage(JSON.stringify(e), 'warning');
		}
		this.search();
		this.user = Token.get();
	},
	methods: {
		handleAdd(e) {
			this.type = '新增';
			if (e && object(e)) this.row = e;
			this.showDialog = true;
		},
		handleEdit(e) {
			this.type = '修改';
			this.row = e;
			this.showDialog = true;
		},
		handleSuccess(e) {
			this.search();
			this.showDialog = false;
		},
		search() {
			this.row = {};
			try {
				this.$api[this.searchUrl](this.formData).then((data) => {
					// console.log('datadatfad', data);
					this.formData.total = data.count || data.total || 0;
					this.list = data.data;
					if (!array(this.list)) this.list = [];
					if (this.addName) {
						this.addName(this.list);
					}
				});
			} catch (e) {
				this.openMessage(`未定义接口 ${this.searchUrl}`, 'warning');
			}
		},
		getRows(rows) {
			this.rows = rows;
		},
		rowClick(row) {
			this.row = row;
		},
		handleDelete(e) {
			this.row = e;
			let jsons = {};
			jsons[this.key] = this.row[this.key];
			this.openConfirm('是否确认删除?', () => {
				this.$api[this.deleteUrl](jsons).then(() => {
					this.search();
				});
			})
		},
	}
}